package com.demo.controller.dataManagemant;

import com.demo.common.ReturnCode;
import com.demo.common.model.Timeline;
import com.demo.service.dataManagement.TimelineService;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.Para;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Record;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Classname TimelineController
 * @Description TODO
 * @Date 2020/3/17 20:15
 * @Created by yuanhang
 */
public class TimelineController extends Controller {
    @Inject
    TimelineService timelineService;

    /**
     * @return
     * @throws
     * @description
     * @author yuanhang
     * @date 2020/3/18 20:27
     * @Param
     */
    public void index() {
        set("router", "/dataManagement/timeline");
        render("index.html");
    }

    /**
     * @return
     * @throws
     * @description
     * @author yuanhang
     * @date 2020/3/18 20:07
     * @Param
     */
    public void tree() {
        Ret ret = new Ret();
        Record record = timelineService.tree();
        List<Record> list = new ArrayList<Record>();
        list.add(record);
        ret.set("code", ReturnCode.SUCCESS.getCode()).set("msg", ReturnCode.SUCCESS.getMsg()).set("data", list);
        renderJson(ret);
    }

    /**
     * @return
     * @throws
     * @description
     * @author yuanhang
     * @date 2020/3/18 20:47
     * @Param
     */
    public void delete(@Para("") Timeline timeline) {
        Ret ret = new Ret();
        if (timeline.delete()) {
            ret.set("code", ReturnCode.SUCCESS.getCode()).set("msg", ReturnCode.SUCCESS.getMsg());
        } else {
            ret.set("code", ReturnCode.ERROR.getCode()).set("msg", ReturnCode.ERROR.getMsg());
        }
        renderJson(ret);
    }

    /**
     * @return
     * @throws
     * @description
     * @author yuanhang
     * @date 2020/3/18 20:47
     * @Param
     */
    public void add(@Para("") Timeline timeline) {
        Ret ret = new Ret();
        timeline.setCreateTime(new Date());
        timeline.setLastUpdateTime(new Date());
        if (timeline.save()) {
            ret.set("code", ReturnCode.SUCCESS.getCode()).set("msg", ReturnCode.SUCCESS.getMsg()).set("data", timeline);
        } else {
            ret.set("code", ReturnCode.ERROR.getCode()).set("msg", ReturnCode.ERROR.getMsg());
        }
        renderJson(ret);

    }

    /**
     * @return
     * @throws
     * @description
     * @author yuanhang
     * @date 2020/3/18 20:47
     * @Param
     */
    public void update(@Para("") Timeline timeline) {
        Ret ret = new Ret();
        timeline.setLastUpdateTime(new Date());
        if (timeline.update()) {
            ret.set("code", ReturnCode.SUCCESS.getCode()).set("msg", ReturnCode.SUCCESS.getMsg());
        } else {
            ret.set("code", ReturnCode.ERROR.getCode()).set("msg", ReturnCode.ERROR.getMsg());
        }
        renderJson(ret);
    }


    /**
    * @description   
    * @author yuanhang
    * @date 2020/3/18 23:19 
    * @Param  
    * @return   
    * @throws   
    */
    public void id(int id) {
        Ret ret = new Ret();
        Timeline timeline = new Timeline();
        timeline = timeline.findById(id);
        ret.set("code", ReturnCode.SUCCESS.getCode()).set("msg", ReturnCode.SUCCESS.getMsg()).set("data", timeline);
        renderJson(ret);
    }
}
